package com.szxb.facebus.util.basics;

import android.util.Log;

import com.szxb.mlog.SLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * 作者：Tangren on 2018-02-28
 * 包名：com.szxb.jcbus.util
 * 邮箱：996489865@qq.com
 * TODO:一句话描述
 */

public class DateUtil {

    private static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", new Locale("zh", "CN"));
    private static SimpleDateFormat main_format = new SimpleDateFormat("yyyy.MM.dd\b\bHH:mm", new Locale("zh", "CN"));

    private static SimpleDateFormat format_3 = new SimpleDateFormat("yyyy-MM-dd", new Locale("zh", "CN"));


    /**
     * @return 得到当前时间yyyyMMddHHmmss
     */
    public static String getCurrentTime() {
        return format.format(new Date());
    }

    public static long getTimeLong(String sTime) {
        try {
            Date date = format.parse(sTime);
            return date.getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
            SLog.d("DateUtil(getTimeLong.java:35)日期解析异常>>sTime=" + sTime + ">>" + e.toString());
            return 0;
        }
    }


    public static Long timeStringToLong(String timeString) {
        try {
            return format.parse(timeString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static Date timeStringToDate(String timeString) {
        try {
            return new Date(format.parse(timeString).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }


    /**
     * 自定义格式获取当前日期
     *
     * @param f .
     * @return .
     */
    public static String getCurrentDate(String f) {
        SimpleDateFormat ft = null;
        try {
            ft = new SimpleDateFormat(f, new Locale("zh", "CN"));
        } catch (Exception e) {
            return format.format(new Date());
        }
        return ft.format(new Date());
    }

    //得到当前日期：yyyy-MM-dd HH:mm:ss
    public static String getCurrentDate() {
        return format.format(new Date());
    }


    public static String getLastDate(String format) {
        SimpleDateFormat ft = null;
        try {
            ft = new SimpleDateFormat(format, new Locale("zh", "CN"));
        } catch (Exception e) {
            return format_3.format(new Date());
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return ft.format(calendar.getTime());
    }


    /**
     * 时钟
     *
     * @return .
     */
    public static String getMainTime() {
        return String.format("%1$s", main_format.format(new Date()));
    }


    /**
     * 文件是否过期
     *
     * @param lastDate 存储的时间
     * @return 是否删除
     */
    public static boolean isDelFile(Date lastDate) {
        return differentDays(lastDate, Calendar.getInstance().getTime()) > 7;
    }


    /**
     * 计算两个日期之间相差的天数
     *
     * @param lastDate    上次存储的日期
     * @param currentDate 现在的日期
     * @return 相差的天数
     */
    private static int differentDays(Date lastDate, Date currentDate) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(lastDate);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(currentDate);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            return day2 - day1;
        }
    }

    //当前时间的前n分钟
    public static String getCurrentDateLast(int var, int time) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(var, -time);// 1分钟之前的时间
        Date beforeD = beforeTime.getTime();
        return format.format(beforeD);
    }


    public static long getMILLISECOND(String startTime, String endTime) {
        try {
            Date startDate = format.parse(startTime);
            Date endDate = format.parse(endTime);
            long l = startDate.getTime() / 1000 - endDate.getTime() / 1000;
            Log.d("DateUtil",
                    "filterOpenID(DateUtil.java:211)相差时间=" + l);
            return l;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
    //获取当时Long型日期
    public static long currentLong() {
        return System.currentTimeMillis() / 1000;
    }


    /**
     * @param time 日期
     * @return yyyyMMddHHmmss转换成yyyy-MM-dd HH:mm:ss
     */
    public static String customFormatTranTime(String time) {
        try {
            Date date = format.parse(time);
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("zh", "CN"));
            return sdf2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * @param currentFormatVar 当前格式
     * @param targetFormatVar  目标格式
     * @param time             日期
     * @return yyyyMMddHHmmss转换成yyyy-MM-dd HH:mm:ss
     */
    public static String customFormatTranTime(String currentFormatVar, String targetFormatVar, String time) {
        try {
            SimpleDateFormat currentFormat = new SimpleDateFormat(currentFormatVar, new Locale("zh", "CN"));
            Date date = currentFormat.parse(time);
            SimpleDateFormat sdf2 = new SimpleDateFormat(targetFormatVar, new Locale("zh", "CN"));
            return sdf2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }


    /**
     * 校准时间
     *
     * @param time .
     */
    public static void setTime(String formatVar, String time, boolean isTip) {
//        try {
//            SimpleDateFormat format = new SimpleDateFormat(formatVar, new Locale("zh", "CN"));
//            Date date = format.parse(time);
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(date);
//            calendar.get(Calendar.YEAR);
//            calendar.get(Calendar.MONTH);
//            calendar.get(Calendar.DATE);
//            calendar.get(Calendar.HOUR);
//            calendar.get(Calendar.MINUTE);
//            calendar.get(Calendar.SECOND);
//
//            int year = calendar.get(Calendar.YEAR);
//            int month = calendar.get(Calendar.MONTH) + 1;
//            int day = calendar.get(Calendar.DAY_OF_MONTH);
//            int hour = calendar.get(Calendar.HOUR_OF_DAY);
//            int min = calendar.get(Calendar.MINUTE);
//            int second = calendar.get(Calendar.SECOND);
//
//            IToolInterface iToolInterface = JCApp.getInstance().getmService();
//            if (iToolInterface != null) {
//                iToolInterface.setDateTime(year, month, day, hour, min, second);
//                if (isTip) {
//                    SoundPoolUtil.play(Constant.MUSIC_BASE);
//                    String toastTip = year + "年" + month + "月" + day + "日" + hour + "时" + min + "分" + second + "秒";
////                    BusToast.showToast(JCApp.getInstance(), "时间更新成功\n" + toastTip, true);
//                }
//            } else {
//                if (isTip) {
////                    BusToast.showToast(JCApp.getInstance(), "校准失败[NULL]", true);
//                }
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            SLog.e("DateUtil(setTime.java:190)时间校准失败>>" + Utils.getExectionStr(e));
//            if (isTip) {
//                BusToast.showToast(JCApp.getInstance(), "校准失败\n" + e.toString(), false);
//            }
//
//        }
    }


    /**
     * @return .
     */
    public static String[] timeRange() {
        String startTime = DateUtil.getTime("yyyyMMddHHmmss", 0, 0, 0, 0);
        String endTime = DateUtil.getTime("yyyyMMddHHmmss", 23, 59, 59, 999);
        return new String[]{startTime, endTime};
    }

    /**
     * @param format            格式：公交卡：yyyyMMddHHmmss,其他yyyy-MM-dd HH:mm:ss
     * @param hour_of_day_value 小时
     * @param minute_value      分钟
     * @param second_value      秒
     * @param millisecond_value 毫秒
     * @return 当前开始时间|结束时间
     */
    public static String getTime(String format, int hour_of_day_value,
                                 int minute_value, int second_value, int millisecond_value) {
        SimpleDateFormat ft = new SimpleDateFormat(format, new Locale("zh", "CN"));
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour_of_day_value);
        calendar.set(Calendar.MINUTE, minute_value);
        calendar.set(Calendar.SECOND, second_value);
        calendar.set(Calendar.MILLISECOND, millisecond_value);
        return ft.format(calendar.getTime());
    }



}
